#include "AbstractQmlModel.h"

namespace TC
{
    namespace Data
    {

        AbstractQmlModel::AbstractQmlModel()
        {
            QObject::connect(this, &AbstractQmlModel::rowsInserted, this, &AbstractQmlModel::countChanged);
            QObject::connect(this, &AbstractQmlModel::rowsRemoved, this, &AbstractQmlModel::countChanged);
        }

        QVariant AbstractQmlModel::get(uint index)
        {
            return _data[index];
        }

        void AbstractQmlModel::add(const QVariantMap &newData)
        {
            beginInsertRows(QModelIndex(), _data.size(), _data.size());
            _data.append(newData);
            endInsertRows();
        }

        void AbstractQmlModel::add(const QList<QVariantMap> &newDatas)
        {
            beginInsertRows(QModelIndex(), _data.size(), _data.size() + newDatas.size() - 1);
            _data.append(newDatas);
            endInsertRows();
        }

        QHash<int, QByteArray> AbstractQmlModel::roleNames() const
        {
            static auto roleNames = QHash<int, QByteArray>{ {0, "model"} };
            return roleNames;
        }

        int AbstractQmlModel::rowCount(const QModelIndex & /*= QModelIndex()*/) const
        {
            return _data.size();
        }

        QVariant AbstractQmlModel::data(const QModelIndex & index, int /*= Qt::DisplayRole*/) const
        {
            return _data.value(index.row());
        }

    }
}

